################################################################################
# script to run maxent contrasting weather and climate

################################################################################
# set the constants

#define some directories
work.dir = '/homes/26/jc184963/weather/weather.all.aust/'
output.dir = '/homes/26/jc184963/weather/weather.all.aust/'

#data file
weather.data.file = 'background.4.weather.work.csv'
climate.data.file = '1975/bioclim1975.csv'
occur.file = 'Savsp_weather_mmyylatlong_post1950.csv'

################################################################################
# do something

#set working directory
setwd(work.dir);getwd()

#read in the occurrence file
occur = read.csv(occur.file);nrow(occur);occur=unique(occur);nrow(occur)
#create weather occur & climate occur
occur.weather = occur
occur.climate = data.frame(SPPCODE=occur$SPPCODE,LATDEC=occur$LATDEC,LONGDEC=occur$LONGDEC); occur.climate=unique(occur.climate)
#read in the climate and weather data for backgrounds
clim.data = read.csv(climate.data.file); names(clim.data)[1:2] = c('LATDEC','LONGDEC');clim.data=unique(clim.data)
weath.data = read.csv(weather.data.file); weath.data = unique(weath.data)

###write out the full occur files in appropriate format
#first weather
occur.weather = merge(occur.weather,weath.data)
occur.weather = na.omit(occur.weather)
occur.weather = occur.weather[c('SPPCODE','LATDEC','LONGDEC',names(occur.weather)[6:length(names(occur.weather))])]
occur.weather = occur.weather[order(occur.weather$SPPCODE,occur.weather$LATDEC,occur.weather$LONGDEC),]
write.csv(occur.weather,'weath.occur.csv',row.names=F,na='')
#now climate
occur.climate = merge(occur.climate,clim.data)
occur.climate = na.omit(occur.climate)
occur.climate = occur.climate[c('SPPCODE','LATDEC','LONGDEC',names(occur.climate)[4:length(names(occur.climate))])]
occur.climate = occur.climate[order(occur.climate$SPPCODE,occur.climate$LATDEC,occur.climate$LONGDEC),]
write.csv(occur.climate,'clim.occur.csv',row.names=F,na='')

###write out the background files in appropriate format
#first the climate data
t.bkgd = unique(weath.data[c('LATDEC','LONGDEC')]);nrow(t.bkgd)
t.bkgd = merge(t.bkgd,clim.data);nrow(t.bkgd)
t.bkgd = data.frame(SPPCODE='bkgd',t.bkgd); write.csv(t.bkgd,'clim.bkgd.csv',row.names=F,na='')
#now the weather data
t.bkgd = data.frame(SPPCODE='bkgd',na.omit(weath.data)); t.bkgd=t.bkgd[-c(2:3)]
write.csv(t.bkgd,'weath.bkgd.csv',row.names=F,na='')

###start setting up and running models
#get the unique species
species = levels(occur$SPPCODE)
#cycle through each of the species
for (spp in species) {
  #create a folder for the species
  spp.folder = paste(output.dir,spp,"/",sep="")
  spp.clim.TG = paste(output.dir,spp,"/climate.TG/",sep="");dir.create(paste(spp.clim.TG,'output/',sep=''), showWarnings=T, recursive=T)
  spp.clim.GRID = paste(output.dir,spp,"/climate.GRID/",sep="");dir.create(paste(spp.clim.GRID,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.folder = paste(output.dir,spp,"/weather/",sep="");dir.create(paste(spp.weath.folder,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc01 = paste(output.dir,spp,"/weather.bc01/",sep="");dir.create(paste(spp.weath.bc01,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc04 = paste(output.dir,spp,"/weather.bc04/",sep="");dir.create(paste(spp.weath.bc04,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc05 = paste(output.dir,spp,"/weather.bc05/",sep="");dir.create(paste(spp.weath.bc05,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc06 = paste(output.dir,spp,"/weather.bc06/",sep="");dir.create(paste(spp.weath.bc06,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc12 = paste(output.dir,spp,"/weather.bc12/",sep="");dir.create(paste(spp.weath.bc12,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc15 = paste(output.dir,spp,"/weather.bc15/",sep="");dir.create(paste(spp.weath.bc15,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc16 = paste(output.dir,spp,"/weather.bc16/",sep="");dir.create(paste(spp.weath.bc16,'output/',sep=''), showWarnings=T, recursive=T)
  spp.weath.bc17 = paste(output.dir,spp,"/weather.bc17/",sep="");dir.create(paste(spp.weath.bc17,'output/',sep=''), showWarnings=T, recursive=T)
  #get the occur records for the species of interest & write them out
  write.csv(occur.climate[occur.climate$SPPCODE==spp,],paste(spp.folder,'occur.clim.csv',sep=''),row.names=F,na='')
  write.csv(occur.weather[occur.weather$SPPCODE==spp,],paste(spp.folder,'occur.weath.csv',sep=''),row.names=F,na='')
  #create the pbs script
  z = file(paste(spp.folder,"maxent.pbs",sep=""),"w")
    cat('#!/bin/bash\n',file=z)
    cat('#!/usr/bin/qsub\n',file=z)
    cat('#PBS -c s\n',file=z)
    cat('#PBS -j oe\n',file=z)
    cat('#PBS -m ae\n',file=z)
    cat('#PBS -N ',spp,'\n',sep="",file=z)
    cat('#PBS -M jc184963@jcu.edu.au\n',file=z)
    cat('#PBS -l walltime=9999:00:00\n',file=z)
    cat('#PBS -l nodes=1:ppn=1 \n',file=z)
    cat('echo "------------------------------------------------------"\n',file=z)
    cat('echo " This job is allocated 2 cpu on "\n',file=z)
    cat('cat $PBS_NODEFILE\n',file=z)
    cat('echo "------------------------------------------------------"\n',file=z)
    cat('echo "PBS: Submitted to $PBS_QUEUE@$PBS_O_HOST"\n',file=z)
    cat('echo "PBS: Working directory is $PBS_O_WORKDIR"\n',file=z)
    cat('echo "PBS: Job identifier is $PBS_JOBID"\n',file=z)
    cat('echo "PBS: Job name is $PBS_JOBNAME"\n',file=z)
    cat('echo "------------------------------------------------------"\n',file=z)
    cat('cd $PBS_O_WORKDIR\n',file=z)
    cat('\n',file=z)
    cat('#set the source for loading Java later\n',file=z)
    cat('source /etc/profile.d/modules.sh\n',file=z)
    cat('\n',file=z)
    cat('#load the java module\n',file=z)
    cat('module load java-sun\n',file=z)
    cat('java -version \n',file=z)
    cat('\n',file=z)
    cat('#run the model\n',file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.clim.TG,'output/" samplesfile="',spp.folder,'occur.clim.csv" environmentallayers="',work.dir,'clim.bkgd.csv" projectionlayers="',work.dir,'1975" nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.clim.GRID,'output/" samplesfile="',spp.folder,'occur.clim.csv" environmentallayers="',work.dir,'1975" nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc01,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc04 -N bc05 -N bc06 -N bc12 -N bc15 -N bc16 -N bc17 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc04,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc01 -N bc05 -N bc06 -N bc12 -N bc15 -N bc16 -N bc17 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc05,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc04 -N bc01 -N bc06 -N bc12 -N bc15 -N bc16 -N bc17 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc06,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc04 -N bc05 -N bc01 -N bc12 -N bc15 -N bc16 -N bc17 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc12,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc04 -N bc05 -N bc06 -N bc01 -N bc15 -N bc16 -N bc17 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc15,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc04 -N bc05 -N bc06 -N bc12 -N bc01 -N bc16 -N bc17 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc16,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc04 -N bc05 -N bc06 -N bc12 -N bc15 -N bc01 -N bc17 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.bc17,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" -N FI -N bc04 -N bc05 -N bc06 -N bc12 -N bc15 -N bc16 -N bc01 nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('java -mx2000m -jar ',work.dir,'maxent.jar outputdirectory="',spp.weath.folder,'output/" samplesfile="',spp.folder,'occur.weath.csv" environmentallayers="',work.dir,'weath.bkgd.csv" nowarnings noaskoverwrite responsecurves nowriteclampgrid novisible autorun \n',sep="",file=z)
    cat('\n',file=z)
  close(z)
  
  setwd(spp.folder)
  system('qsub maxent.pbs')
  #system('sleep 3')
  setwd(work.dir)
  
}
